
var BattleUnitHp = cc.Node.extend({

	imgHp : null,
    imgBg : null,
    imgSlow : null, // 减血时候渐变的那张图

    hp : 0, // 血量
    mhp : 0,

    ctor : function (hp, mhp, side) {
		this._super();

        this.hp = hp;
        this.mhp = mhp;
        this.hpSlow = mhp;

        var sp = cc.Sprite.create('res/battle/hp_bg.png');
        sp.setOpacity(150);
        this.addChild(sp);
        this.imgBg = sp;

        sp = cc.Sprite.create('res/battle/hp_slow.png');
        this.addChild(sp);
        this.imgSlow = sp;

        sp = cc.Sprite.create('res/battle/hp.png');
        //sp.setOpacity(150);
        this.addChild(sp);
        this.imgHp = sp;

        // 调整大小
        this.setScaleX(0.3);
        this.setScaleY( side == 0 ? 0.5 : -0.5 );
	},

    addHp : function (hp) {
        this.setHp(this.hp + hp);
    },

    setHp : function (hp) {
        cc.log('setHp:'+hp);
            
        // 渐变条
        this.drawHpSlow(this.hp, hp, this.mhp);

        // hp条
        this.hp = hp;
        var p = hp / this.mhp;
        glb.displayTexturePercent(this.imgHp, p);
    },

    drawHpSlow : function (hp, newhp, mhp) {

        var _this = this;
        function _func (dt) {
            var p = newhp / mhp;
            glb.displayTexturePercent(_this.imgSlow, p);
        }
        this.scheduleOnce(_func, 3);    // 3秒后和hp一样
    },

    show : function (isShow) {
        var time = 0.5;
        var f = isShow ? 255 : 0;
        var fade = cc.FadeTo.create(time, f);                  // 渐变
        var fade1 = cc.FadeTo.create(time, f);
        var fade2 = cc.FadeTo.create(time, f);
        
        this.imgHp.runAction(fade);
        this.imgBg.runAction(fade1);
        this.imgSlow.runAction(fade2);
    }

});

